
#include "QOpenCVWidget.h"

// Constructor
QOpenCVWidget::QOpenCVWidget(QWidget *parent) : QWidget(parent) {
    layout = new QVBoxLayout;
    imagelabel = new QLabel;
    QImage dummy(100,100,QImage::Format_RGB888);
    image = dummy;
    layout->addWidget(imagelabel);
    for (int x = 0; x < 100; x ++) {
        for (int y =0; y < 100; y++) {
            image.setPixel(x,y,qRgb(x, y, y));
        }
    }
    imagelabel->setPixmap(QPixmap::fromImage(image));

    setLayout(layout);
}

QOpenCVWidget::~QOpenCVWidget(void) {
    
}

void QOpenCVWidget::putImage(IplImage *cvimage) {
    int cvIndex, cvLineStart;
    //const int nScanBytes = cvimage->width * cvimage->nChannels;
    // switch between bit depths
    switch (cvimage->depth) {
        case IPL_DEPTH_8U:
            switch (cvimage->nChannels) {
                case 3:
                    if ( (cvimage->width != image.width()) || (cvimage->height != image.height()) ) {
                        QImage temp(cvimage->width, cvimage->height, QImage::Format_RGB888);
                        image = temp;
                    }
                    cvIndex = 0; cvLineStart = 0;
                    for (int y = 0; y < cvimage->height; y++) {
                        cvIndex = cvLineStart;
                        if(false)
                        {
                            unsigned char red,green,blue;
                            for (int x = 0; x < cvimage->width; x++) {
                                // DO it
                                red = cvimage->imageData[cvIndex+2];
                                green = cvimage->imageData[cvIndex+1];
                                blue = cvimage->imageData[cvIndex+0];

                                image.setPixel(x,y,qRgb(red, green, blue));
                                cvIndex += 3;
                            }
                        }
                        else
                        {
                            unsigned char red,green,blue;
                            uchar * pScan = image.scanLine(y);
                            for (int x = 0; x < cvimage->width*cvimage->nChannels;) {
                                // DO it
                                red = cvimage->imageData[cvIndex+2];
                                green = cvimage->imageData[cvIndex+1];
                                blue = cvimage->imageData[cvIndex+0];

                                int ired = static_cast<int>(red)*3/2;

                                if(ired>255)
                                    ired=255;

                                pScan[x++] = ired;
                                pScan[x++] = green;
                                pScan[x++] = blue;

                                cvIndex += 3;
                            }
                            //memcpy(pScan, cvimage->imageData + cvIndex, nScanBytes);
                        }

                        cvLineStart += cvimage->widthStep;
                    }
                    break;
                default:
                    printf("This number of channels is not supported\n");
                    break;
            }
            break;
        default:
            printf("This type of IplImage is not implemented in QOpenCVWidget\n");
            break;
    }
    imagelabel->setPixmap(QPixmap::fromImage(image));    
}

